<?php

namespace app\store\controller\ditui;

use app\store\controller\Controller;
use app\store\model\Store as StoreModel;
use think\Db;

class Dtzidian extends Controller
{
    public function getArr($type){
        $zhArr = array("1次转化","2次转化","3次转化","4次转化","5次转化","6次转化","7次转化","8次转化","9次转化","10次转化");
        $zhColor = array("#85ce61","#85ce61","#85ce61","#85ce61","#85ce61","#85ce61","#85ce61","#85ce61","#85ce61","#85ce61");

        $fbArrp = array("第1班","第2班","第3班","第4班","第5班","第6班","第7班","第8班","第9班","第10班",
            "第11班","第12班","第13班","第14班","第15班","第16班","第17班","第18班","第19班","第20班",
            "第21班","第22班","第23班","第24班","第25班","第26班","第27班","第28班","第29班","第30班");

        $schoolArr = array("第1学校","第2学校","第3学校","第4学校","第5学校","第6学校","第7学校","第8学校","第9学校","第10学校",
            "第11学校","第12学校","第13学校","第14学校","第15学校","第16学校","第17学校","第18学校","第19学校","第20学校");

        $keArr = array("第1课程#10","第2课程#10","第3课程#10","第4课程#10","第5课程#10","第6课程#10","第7课程#10","第8课程#10","第9课程#10","第10课程#10");
        if($type==1){
            return $zhArr;
        }
        if($type==2){
            return $fbArrp;
        }
        if($type==3){
            return $schoolArr;
        }
        if($type==4){
            return $keArr;
        }
        if($type==11){
            return $zhColor;
        }
    }
    /** 地推专员数据列表，此为主函数，作为权限主键使用，其他为public函数的是其他类要调用的方法 */
    public function zidian()
    {
        $zhArr = $this->getArr(1);
        $zh_txtstr_old = implode(',', $zhArr);

        $zhccArr = $this->getArr(11);
        $zh_color_old = implode(',', $zhccArr);

        $fbArrp = $this->getArr(2);
        $fb_txtstr_old = implode(',', $fbArrp);

        $schoolArr = $this->getArr(3);
        $sc_school_old = implode(',', $schoolArr);

        $keArr = $this->getArr(4);
        $keArr_old_str = implode(',', $keArr);

        //先查询此店铺中有没有字典，没有字典就入库
        $zidianObj = Db::name('ditui_zidian')->where('dz_shopid', $this->ad_shopid)->find();
        if(!$zidianObj){
            $inArr['dz_shopid'] = $this->ad_shopid;
            $inArr['dz_type'] = 1;
            $inArr['dz_zhtxtstr'] = $zh_txtstr_old;
            $inArr['dz_zhcolor'] = $zh_color_old;
            $inArr['dz_fbtxtstr'] = $fb_txtstr_old;
            $inArr['dz_schoolstr'] = $sc_school_old;
            $inArr['dz_kkccstr'] = $keArr_old_str;
            $inArr['wxapp_id'] = $this->wxapp_id;
            $inArr['dz_addtime'] = date('Y-m-d H:i:s',time());
            Db::name('ditui_zidian')->insert($inArr);
            $zidianObj = $inArr;
        }

        $back['zhanghua_oldArr'] = $zhArr;
        $back['zhanghua_color_oldArr'] = $zhccArr;
        $back['fenban_oldArr'] = $fbArrp;
        $back['school_oldArr'] = $schoolArr;
        $back['keArr_old'] = $keArr;

        $back['new_zhuanhuaArr'] = explode(',', $zidianObj['dz_zhtxtstr']);
        $back['new_color_zhuanhuaArr'] = explode(',', $zidianObj['dz_zhcolor']);
        $back['new_fenbanArr'] = explode(',', $zidianObj['dz_fbtxtstr']);
        $back['new_schoolArr'] = explode(',', $zidianObj['dz_schoolstr']);
        $back['keArr_new'] = explode(',', $zidianObj['dz_kkccstr']);

        $zidianObj['dz_kkccstr']==null ? $back['keArr_new'] = $keArr : "";
        //ddd($back);

        return $this->fetch('ditui/zidian', ['data' =>$back]);
    }

    /**处理组转新的结果字符串 */
    private function getNewArr($strTxt, $type){
        $strArr = array();
        if($strTxt!='' && $strTxt!=null){
            $strArr = explode(',', $strTxt);
        }

        $back = array();
        foreach ($strArr as $kk => $vv) {
            $back['tab_'.($kk+1)] = $vv;
        }
        return $back;
    }

    /**处理生成新的字典*/
    public function opmnewzd(){
        $paramArr = $this->request->param();
        $_inNewArr = isset($paramArr['inNewArr']) ? $paramArr['inNewArr'] : "";
        $type = isset($paramArr['type']) ? $paramArr['type'] : 1;
        if($_inNewArr!=''){
            $inNewArr = array();
            //处理传递过来为空的字段
            foreach ($_inNewArr as $vv) {
                if(trim($vv)!=''){
                    $inNewArr[] = $vv;
                }
            }
            if($type==1){
                $upArr['dz_zhtxtstr'] = implode(',', $inNewArr);;
            }
            if($type==11){
                $upArr['dz_zhcolor'] = implode(',', $inNewArr);;
            }
            if($type==2){
                $upArr['dz_fbtxtstr'] = implode(',', $inNewArr);;
            }
            if($type==3){
                $upArr['dz_schoolstr'] = implode(',', $inNewArr);;
            }
            if($type==4){
                $upArr['dz_kkccstr'] = implode(',', $inNewArr);;
            }
            Db::name('ditui_zidian')->where('dz_shopid', $this->ad_shopid)->update($upArr);
            return $this->renderSuccess('更新成功');
        }
        else{
            return $this->renderError('字典中存在空数据');
        }
    }

}
